import 'package:flutter/material.dart';
import 'package:sow_app/view/common/chip_filter.dart';
import '../common/novel/novel_list_tile.dart';
import 'classify/classify_page.dart';

import 'package:sow_app_core/sow_app_core.dart';
import 'package:sow_app_i18n/sow_app_i18n.dart';

import '../../presenter/state_library_presenter.dart';
import 'search/search_page.dart';

class LibraryPage extends StatefulWidget implements AppPage {
  const LibraryPage(
      {Key? key, required this.controller, required this.presenter})
      : super(key: key);

  final LibraryController controller;
  final StateLibraryPresenter presenter;
  @override
  State<LibraryPage> createState() => _LibraryPageState();
}

class _LibraryPageState extends State<LibraryPage> {
  @override
  void initState() {
    widget.presenter.presenterContext = context;
    widget.presenter.presenterSetState = setState;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: false,
        title: const Text(I18n.library),
        actions: [
          IconButton(
              onPressed: () {
                Navigator.pushNamed(context, SearchPage.routeName);
              },
              icon: const Icon(Icons.search)),
        ],
      ),
      body: ListView(
        children: [
          Padding(
            padding: const EdgeInsets.only(left: 8.0, top: 8.0),
            child: Text(
              I18n.rank,
              style: Theme.of(context).textTheme.headline5,
            ),
          ),
          ChipFilter(
              onItemSelected: (p0) {
                widget.presenter.rankIndex = p0.value!;
                widget.controller.init();
              },
              selections: widget.presenter.rankTabs),
          SizedBox(
            height: 210,
            child: IndexedStack(
              index: widget.presenter.rankIndex,
              children: [
                GridView.count(
                  childAspectRatio: 1 - 3 / 4,
                  scrollDirection: Axis.horizontal,
                  crossAxisCount: 3,
                  children: widget.presenter.recommendRank
                      .map((e) => NovelListTile(novel: e))
                      .toList(),
                ),
                GridView.count(
                  childAspectRatio: 1 - 3 / 4,
                  scrollDirection: Axis.horizontal,
                  crossAxisCount: 3,
                  children: widget.presenter.endBookRank
                      .map((e) => NovelListTile(
                            novel: e,
                          ))
                      .toList(),
                ),
                GridView.count(
                  childAspectRatio: 1 - 3 / 4,
                  scrollDirection: Axis.horizontal,
                  crossAxisCount: 3,
                  children: widget.presenter.hotRank
                      .map((e) => NovelListTile(
                            novel: e,
                          ))
                      .toList(),
                ),
              ],
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(left: 8.0, top: 16.0, right: 8.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  I18n.hot_tag,
                  style: Theme.of(context).textTheme.headline5,
                ),
                SizedBox(
                  height: 32,
                  child: TextButton(
                    onPressed: () {
                      Navigator.pushNamed(context, ClassifyPage.routeName);
                    },
                    child: const Text(
                      I18n.more_tags,
                    ),
                  ),
                )
              ],
            ),
          ),
          ChipFilter(
              onItemSelected: (p0) {
                widget.presenter.tagIndex = p0.value!;
                widget.controller.init();
              },
              selections: widget.presenter.tagTabs
                  .map((e) =>
                      ChipFilterEntry(e.classifyName, int.parse(e.classifyId)))
                  .toSet()),
          SizedBox(
            height: 210,
            child: GridView.count(
              childAspectRatio: 1 - 3 / 4,
              scrollDirection: Axis.horizontal,
              crossAxisCount: 3,
              children: widget.presenter.novelsByTag
                  .map((e) => NovelListTile(
                        novel: e,
                      ))
                  .toList(),
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(left: 8.0, top: 16.0),
            child: Text(
              I18n.recommend,
              style: Theme.of(context).textTheme.headline5,
            ),
          ),
          ChipFilter(
              onItemSelected: (p0) {
                widget.presenter.otherIndex = p0.value!;
                widget.controller.init();
              },
              selections: widget.presenter.otherTabs),
          SizedBox(
            height: 560,
            child: IndexedStack(
              index: widget.presenter.otherIndex,
              children: [
                GridView.count(
                  childAspectRatio: 1 - 3 / 4,
                  scrollDirection: Axis.horizontal,
                  crossAxisCount: 8,
                  children: widget.presenter.hignScore
                      .map((e) => NovelListTile(
                            novel: e,
                          ))
                      .toList(),
                ),
                GridView.count(
                  childAspectRatio: 1 - 3 / 4,
                  scrollDirection: Axis.horizontal,
                  crossAxisCount: 8,
                  children: widget.presenter.newCome
                      .map((e) => NovelListTile(
                            novel: e,
                          ))
                      .toList(),
                ),
                // GridView.count(
                //   childAspectRatio: 1 - 3 / 4,
                //   scrollDirection: Axis.horizontal,
                //   crossAxisCount: 8,
                //   children: widget.presenter.guessYouLike
                //       .map((e) => NovelListTile(
                //             novel: e,
                //           ))
                //       .toList(),
                // ),
              ],
            ),
          ),
        ],
      ),
      // body:
    );
  }
}
